<?


class CurrencyUtil
{
	/**
	 * Get currency number in full
	 *
	 * @param double $number
	 * @param bool $upper
	 * @return string
	 */
	public static function InFull($number, $upper = false)
	{
	    $singular = array("centavo", "real", "mil", "milhão", "bilhão", "trilhão", "quatrilhão");
	    $plural = array("centavos", "reais", "mil", "milhões", "bilhões", "trilhões", "quatrilhões"); 
	
	    $c = array("", "cem", "duzentos", "trezentos", "quatrocentos", "quinhentos", "seiscentos", "setecentos", "oitocentos", "novecentos"); 
	    $d = array("", "dez", "vinte", "trinta", "quarenta", "cinquenta", "sessenta", "setenta", "oitenta", "noventa"); 
	    $d10 = array("dez", "onze", "doze", "treze", "quatorze", "quinze", "dezesseis", "dezesete", "dezoito", "dezenove"); 
	    $u = array("", "um", "dois", "três", "quatro", "cinco", "seis", "sete", "oito", "nove"); 
	
	    $z = 0; 
	    $number = number_format($number, 2, ".", ".");
	    $inteiro = explode(".", $number); 
	    for($i=0; $i < count($inteiro); $i++){
	        for($ii=strlen($inteiro[$i]);$ii<3;$ii++){
	            $inteiro[$i] = "0".$inteiro[$i];
	        }
	    }
	    $fim = count($inteiro) - ($inteiro[count($inteiro)-1] > 0 ? 1 : 2);
	    for ($i=0;$i<count($inteiro);$i++) {
	        $number = $inteiro[$i]; 
	        $rc = (($number > 100) && ($number < 200)) ? "cento" : $c[$number[0]]; 
	        $rd = ($number[1] < 2) ? "" : $d[$number[1]]; 
	        $ru = ($number > 0) ? (($number[1] == 1) ? $d10[$number[2]] : $u[$number[2]]) : ""; 
	
	        $r = $rc.(($rc && ($rd || $ru)) ? " e " : "").$rd.(($rd && $ru) ? " e " : "") . $ru;
	        $t = count($inteiro)-1-$i; 
	        $r .= $r ? " ".($number > 1 ? $plural[$t] : $singular[$t]) : ""; 
	        if ($number == "000")$z++; elseif ($z > 0) $z--; 
	        if (($t==1) && ($z>0) && ($inteiro[0] > 0)) $r .= (($z>1) ? " de " : "").$plural[$t]; 
	        if ($r) $rt = $rt . ((($i > 0) && ($i <= $fim) && ($inteiro[0] > 0) && ($z < 1)) ? ( ($i < $fim) ? ", " : " e ") : " ") . $r; 
	    }
	    if(!$upper){ return($rt ? $rt : "zero"); 
	    } else {
	    	if ($rt){
	    		$rt=ereg_replace(" E "," e ",ucwords($rt));
	    	}
	    	return (($rt) ? ($rt) : "Zero"); 
	    }
	}
	/**
	 * Convert a double number in dollar currency format
	 *
	 * @param double $number
	 * @return string
	 */
	public static function Dollar($number)
	{
		return number_format($number, 2, ".", ",");
	}
	/**
	 * Convert a double number in real(brazil) currency format
	 *
	 * @param double $number
	 * @return string
	 */
	public static function Real($number)
	{
		return number_format($number, 2, ",", ".");
	}
	/**
	 * Convert from Real currency to Double value
	 *
	 * @param string $number
	 * @return double
	 */
	public static function Double($number)
	{
		if (is_double($number))
		{
			return $number;
		}
		if (strpos($number, ',') !== false)
		{
			return doubleval(str_replace(',', '.', str_replace('.', '', $number)));
		}
		return doubleval($number);
	}
	
	/**
	 * Colocar zeros em um numero. Ex: 000001123
	 *
	 * @param float $number
	 * @param int $intPart
	 * @param int $floatPart
	 * @param string $dec_point
	 * @param string $thousands_sep
	 * @return string
	 */
	public static function LeadingZero($number, $intPart, $floatPart = null, $dec_point = null, $thousands_sep = null) 
	{
  		$formattedNumber = abs($number);
  		if (!is_null($floatPart)) 
		{
   			$formattedNumber = number_format($formattedNumber, $floatPart, $dec_point, $thousands_sep);
   		}
		$formattedNumber = str_repeat("0",($intPart + -1 - floor(log10($formattedNumber)))).$formattedNumber;
		return $formattedNumber;
	}
}
?>